/**
 * @type type: 1-团长 2-副团长
 * @status 门店状态(0.申请中，1.运营中，2.休店中，98.禁用，99.已拒绝)
 */
var util = require("../../utils/util.js");
var api = require("../../config/api.js");
var app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        type: 1,
        title: "",
        info: {},
        sellData: {},
        addressVo: {},
        manageList: [
            {
                title: "订单中心",
                imageUrl:
                    "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/center/1.png",
                isLeader: true,
                url: "/Surroundings/ordercenter/ordercenter"
            },
            {
                title: "店员管理",
                imageUrl:
                    "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/center/2.png",
                isLeader: true,
                url: "/Surroundings/staff/staff"
            },
            {
                title: "认证申请",
                imageUrl:
                    "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/center/6.png",
                isLeader: true,
                url: "/Surroundings/certification/certification"
            },
            {
                title: "扫码核销",
                imageUrl:
                    "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/center/7.png",
                isLeader: false,
                url: ""
            },
        ],
        userInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let manageList = this.data.manageList;
        if(options.rs == 2){
            // 副团长
            manageList = manageList.filter(item => !item.isLeader)
        }
        this.setData({
            navH: app.globalData.navHeight,
            type: options.rs,
            manageList
        });
        wx.setNavigationBarTitle({
            title: this.data.title,
        });
        this.setData({
            userInfo: wx.getStorageSync("userInfo")
        })
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getMyLeaderInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    getMyLeaderInfo() {
        wx.showLoading({
            title: "正在加载",
        });
        const userInfo = wx.getStorageSync("userInfo");
        util.request(api.surroundings.getMerchantInfoById + "/" + userInfo.merchantId, {}, "POST")
            .then((res) => {
                wx.hideLoading();
                // const 
                this.setData({
                    info: res.result,
                    // status 商家状态(0.休店，1.营业)
                    statusText: res.result.status == 1 ? "营业" : "休店"
                });
                wx.setStorageSync("addressVo", res.result.addressVo);
            })
            .catch((err) => {
                wx.hideLoading();
            });
    },
    goTo(e){
        let { url, title } = e.currentTarget.dataset;
        const { info } = this.data;
        if(title === "扫码核销"){
            this.scanCode();
            return ;
        }
        if(title === "认证申请"){
            // 认证状态 0-未交定金 1.申请中 2.申请成功 3.退款中 4.已退押金 99.已拒绝
            switch(info.attestationType){
                case 1:
                    url = "/Leader/application/application-result/application-result?rs=0";
                    break;
                case 3:
                    url = "/Leader/application/application-result/application-result?rs=0&title=认证退款";
                    break;
                case 99:
                    url = "/Leader/application/application-result/application-result?rs=1";
                    break;
                case 2:
                    url = "/Surroundings/status/status?status=3"
                    break;
                default:
                    url = "/Surroundings/certification/certification"
            }
        }
        // if(title === "申请休店"){
        //     if(!this.data.addressVo.id){
        //         return wx.showToast({
        //             title: "请选择提货地址",
        //             icon: "none"
        //         })
        //     }
        //     switch(this.data.addressVo.status){
        //         case 0: //申请中
        //             url = "/Leader/application/application-result/application-result?rs=0"
        //             break;
        //         case 99: // 已拒绝
        //             url = "/Leader/application/application-result/application-result?rs=1"
        //             break;
        //         case 2: // 休店中
        //             url += "?id=" + this.data.addressVo.id + "&rs=1"
        //             break;
        //         case 1: //运营中
        //             url += "?id=" + this.data.addressVo.id + "&rs=0"
        //     }
        // }
        wx.navigateTo({
            url
        })
    },
    scanCode(){
        wx.scanCode({
            success (result) {
              console.log("result", result);
              wx.showLoading();
              const orderId = result.result;
              util.request(api.surroundings.pinProduct + "/" + orderId, {}, "PUT").then(res=>{
                wx.hideLoading();
                wx.showToast({
                    title: "核销成功",
                    icon: "success"
                })
              }).catch(err=>{
                  wx.showToast({
                      title: err.message,
                      icon: "none"
                  })
              })
            },
            fail(err){
                console.log(err);
            }
        })
    }
});
